
import java.io.InputStream;


public class Utils {
    static byte[][] str;
    static int maxString = 0;

    public static String getString(int id) {
        if(id >= maxString) {
            System.out.println("max id is " + maxString);
            return null;
        }
        else {
            return new String(str[id]);
        }
    }

    public static byte[] loadPackage(String res, int id) {
        InputStream array = null;
        byte[] tmp = null;
        try {
            System.out.println("loadPackage(/"+res+", "+id+")");
            array = "".getClass().getResourceAsStream("/"+res);
            tmp = new byte[array.available()];
            System.out.println("available: " + array.available());
            array.read(tmp);
            int skip = 4;
            int off = (tmp[(id<<2)+skip]<<3)
                + (tmp[(id<<2)+1+skip]<<2)
                + (tmp[(id<<2)+2+skip]<<1)
                + (tmp[(id<<2)+3+skip]);
            System.out.println("off: " + off);
            int off1 = id < skip-1 ?
                    (tmp[((id+1)<<2)+skip]<<3)
                        + (tmp[((id+1)<<2)+1+skip]<<2)
                        + (tmp[((id+1)<<2)+2+skip]<<1)
                        + (tmp[((id+1)<<2)+3+skip])
                    : array.available();
            System.out.println("off1: " + off1);
            byte[] ret = new byte[off1-off];
            System.arraycopy(tmp, off, ret, 0, (off1-off));
            return ret;
        }
        catch(Exception e) {
            System.out.println("Exc: loadPackage: " + e.getMessage());
            return null;
        }
    }

    public static void loadText(String res) {
        System.out.println("loadText()");
        byte[] offsets = null;
        byte[] strings = null;
        int[] off = null;

        System.out.println("loadText(offsets)");
        try {
            offsets = loadPackage(res, TEXTS_EN.TEXTS_EN_MENU_EN_offset);
            System.out.println("length: " + offsets.length);
            off = new int[offsets.length>>2];
            for(int i=0; i<offsets.length>>2; i++) {
                off[i] = (offsets[(i<<2)]<<3)
                        + (offsets[(i<<2)+1]<<2)
                        + (offsets[(i<<2)+2]<<1)
                        + (offsets[(i<<2)+3]);
                System.out.println("off["+i+"]: " + off[i]);
            }
        }
        catch(Exception e) {
            System.out.println("Exc: loadText: " + e.getMessage());
        }

        System.out.println("loadText(strings)");
        try {
            strings = loadPackage(res, TEXTS_EN.TEXTS_EN_MENU_EN);
            System.out.println("lenth: " + strings.length);
        }
        catch(Exception e) {
            System.out.println("Exc: loadText2: " + e.getMessage());
        }

        if(offsets == null || strings == null) {
            System.out.println("texts and/or ofsets not loaded");
            return;
        }
        System.out.println("loadText(parse strings)");
        str = new byte[off.length][];
        int i = 0;
        int init = 0;
        int end = 0;
        while(i<off.length) {
            init = off[i];
            end = i < off.length-1 ? off[i+1] : strings.length-1;
            System.out.println("loadText(i: " + i + " init: " + init + " end: " + end + " size: " + (end - init) + ")");
            str[i] = new byte[end - init];
            System.arraycopy(strings, off[i], str[i], 0, str[i].length);
            System.out.println("loadText(" + new String(str[i]) + ")");
            i++;
        }

        maxString = str.length;

        System.out.println("loadText() end");
    }
}
